// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.

using System;

namespace Azure.AI.Agents.Persistent;

public class TextAnnotationUpdate
{
    /// <summary>
    /// The index of the content item that this annotation applies to.
    /// </summary>
    public int ContentIndex
        => _fileSearchCitation?.Index
        ?? _codeCitation?.Index
        ?? _uriAnnotation?.Index
        ?? (_internalAnnotation?.SerializedAdditionalRawData.TryGetValue("index", out BinaryData indexData) == true
            ? int.Parse(indexData.ToString())
            : -1);

    /// <summary>
    /// The index in the message content at which the citation begins.
    /// </summary>
    public int? StartIndex
        => _fileSearchCitation?.StartIndex
        ?? _codeCitation?.StartIndex
        ?? _uriAnnotation?.StartIndex
        ?? (_internalAnnotation?.SerializedAdditionalRawData.TryGetValue("start_index", out BinaryData indexData) == true
            ? int.Parse(indexData.ToString())
            : null);

    /// <summary>
    /// The index in the message content at which the citation ends.
    /// </summary>
    public int? EndIndex
        => _fileSearchCitation?.EndIndex
        ?? _codeCitation?.EndIndex
        ?? _uriAnnotation?.EndIndex
        ?? (_internalAnnotation?.SerializedAdditionalRawData.TryGetValue("start_index", out BinaryData indexData) == true
            ? int.Parse(indexData.ToString())
            : null);

    /// <summary>
    /// The text in the message content that should be replaced.
    /// </summary>
    public string TextToReplace
        => _fileSearchCitation?.Text
        ?? _codeCitation?.Text
        ?? (_internalAnnotation?.SerializedAdditionalRawData?.TryGetValue("text", out BinaryData textData) == true
            ? textData.ToString()
            : null);

    /// <summary>
    /// The ID of the file cited by the <c>file_search</c> tool for this annotation.
    /// </summary>
    public string InputFileId => _fileSearchCitation?.FileCitation?.FileId;

    /// <summary>
    /// The url from url annotation if any.
    /// </summary>
    public string Url => _uriAnnotation?.UriCitation?.Uri;

    /// <summary>
    /// The title from url annotation if any.
    /// </summary>
    public string Title => _uriAnnotation?.UriCitation?.Title;

    /// <summary>
    /// The ID of the file that was generated by the <c>code_interpreter</c> tool for this citation.
    /// </summary>
    public string OutputFileId => _codeCitation?.FilePath?.FileId;

    internal readonly MessageDeltaTextAnnotation _internalAnnotation;
    private readonly MessageDeltaTextFileCitationAnnotation _fileSearchCitation;
    private readonly MessageDeltaTextFilePathAnnotation _codeCitation;
    private readonly MessageDeltaTextUriCitationAnnotation _uriAnnotation;

    internal TextAnnotationUpdate(MessageDeltaTextAnnotation internalAnnotation)
    {
        _internalAnnotation = internalAnnotation;
        _fileSearchCitation = internalAnnotation as MessageDeltaTextFileCitationAnnotation;
        _codeCitation = internalAnnotation as MessageDeltaTextFilePathAnnotation;
        _uriAnnotation = internalAnnotation as MessageDeltaTextUriCitationAnnotation;
    }
}
